﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Desktop.View;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara3084ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para3084ViewModel : ViewModelBase, IPara3084ViewModel
    {
        private Para3084DAO paramDao;

        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView paraCollectionDetail;
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private ICollectionView lineList;
        public ICollectionView LineList
        {
            get
            {
                return lineList;
            }
            set
            {
                this.lineList = value;
                this.RaisePropertyChangedEvent("LineList");
            }
        }

        private ICollectionView stationList;
        public ICollectionView StationList
        {
            get
            {
                return stationList;
            }
            set
            {
                this.stationList = value;
                this.RaisePropertyChangedEvent("StationList");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        private string fileName;
        public string FileName
        {
            get
            {
                return this.fileName;
            }
            set
            {
                this.fileName = value;
                this.RaisePropertyChangedEvent("FileName");
            }
        }

        private int xPoint;
        public int XPoint
        {
            get
            {
                return this.xPoint;
            }
            set
            {
                this.xPoint = value;
                this.RaisePropertyChangedEvent("XPoint");
            }
        }

        private int yPoint;
        public int YPoint
        {
            get
            {
                return this.yPoint;
            }
            set
            {
                this.yPoint = value;
                this.RaisePropertyChangedEvent("YPoint");
            }
        }

        private short length;
        public short Length
        {
            get
            {
                return this.length;
            }
            set
            {
                this.length = value;
                this.RaisePropertyChangedEvent("Length");
            }
        }

        private short width;
        public short Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
                this.RaisePropertyChangedEvent("Width");
            }
        }

        private ImageBrush background;
        public ImageBrush Background
        {
            get
            {
                return this.background;
            }
            set
            {
                this.background = value;
                this.RaisePropertyChangedEvent("Background");
            }
        }

        private int fullScreenWith;
        public int FullScreenWith
        {
            get
            {
                return this.fullScreenWith;
            }
            set
            {
                this.fullScreenWith = value;
                this.RaisePropertyChangedEvent("FullScreenWith");
            }
        }

        private int fullScreenHeight;
        public int FullScreenHeight
        {
            get
            {
                return this.fullScreenHeight;
            }
            set
            {
                this.fullScreenHeight = value;
                this.RaisePropertyChangedEvent("FullScreenHeight");
            }
        }

        private ObservableCollection<CurrentLine> currentLines;


        private PhotoGraphView3084 photoGraphView3084;

        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }

        public DelegateCommand LoadImageCommand { get; private set; }
        public DelegateCommand ConfirmCoordinateCommand { get; private set; }

        [ImportingConstructor]
        public Para3084ViewModel()
        {
            paramDao = new Para3084DAO();
            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            if (paramDao.GetAllVersion().Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("当前参数无任何参数版本");
            }
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

            EditGroupBoxVisible = Visibility.Hidden;

            currentLines = paramDao.GetCurrentLine();
            LineList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentLine());
            LineList.CurrentChanged += LineList_CurrentChanged;

            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);
            this.LoadImageCommand = new DelegateCommand(this.LoadImage);
            this.ConfirmCoordinateCommand = new DelegateCommand(this.ConfirmCoordinate);
        }

        private void LoadImage()
        {
            ApplictionDatacontext.TopPoint = null;
            ApplictionDatacontext.BottomPoint = null;

            photoGraphView3084 = new PhotoGraphView3084(this);

            Background = new ImageBrush();


            Background.ImageSource =
                new BitmapImage(
                    new Uri(this.FileName, UriKind.Relative)
                );
            try
            {
                Background.ImageSource =
                new BitmapImage(
                    new Uri(this.FileName, UriKind.Relative)
                );
                System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(this.FileName);
                this.FullScreenHeight = imgPhoto.Height;
                this.FullScreenWith = imgPhoto.Width;
                photoGraphView3084.Show();
            }
            catch (Exception e)
            {
                MessageBox.Show("加载图片出错!" + e.Message);
            }
        }

        private void ConfirmCoordinate()
        {
            if (ApplictionDatacontext.TopPoint != null)
            {
                Point? topPoint = ApplictionDatacontext.TopPoint;
                this.XPoint = (int)topPoint.Value.X;
                this.YPoint = (int)topPoint.Value.Y;
                this.Length = 49;
                this.Width = 49;
                photoGraphView3084.Close();
            }
            else
            {
                MessageBox.Show("未框选坐标!请进行框选");
            }

        }

        private void AddParam()
        {
            if (InfoVerList.CurrentItem == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("请添加参数版本！才可进行添加");
            }
            else
            {
                EditGroupBoxVisible = Visibility.Visible;
            }
        }

        public void SaveParam()
        {
            if (Confirm())
            {
                Para3084 param = new Para3084();
                Para3084Pk paramPk = new Para3084Pk();
                int tmp = (int)InfoVerList.CurrentItem;
                paramPk.infoVer = Int16.Parse(tmp.ToString());
                paramPk.lineid = Int32.Parse((LineList.CurrentItem as CurrentLine).LineID);
                paramPk.statnid = Int16.Parse((StationList.CurrentItem as CurrentStation).StationID.Substring(2,2));
                param.para3084Pk = paramPk;
                if (this.FileName != null)
                {
                    param.filename = Path.GetFileName(this.FileName);
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("信息录入不全，无法完成保存!");
                    return;
                }
                param.length = this.Length;
                param.XPoint = this.XPoint;
                param.YPoint = this.YPoint;
                param.width = this.Width;
                param.resv2 = 0;
                param.recNum = 1;
                param.resv11 = 0;
                param.resv12 = 0;
                param.resv13 = 0;
                param.resv14 = 0;
                param.stationcount = (short)paramDao.GetCurrentStation((LineList.CurrentItem as CurrentLine).LineID.Trim()).Count;
                //编辑参数
                ResultValue result = paramDao.CreateObject(param);
                if (result.Code == 0)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                    ReflashDataGrid();

                    CanclEditParam();
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                }
            }
        }

        private void CanclEditParam()
        {
            this.EditGroupBoxVisible = Visibility.Hidden;
            //将控件置为空
        }

        private bool Confirm()
        {
            bool flag = true;
            if (InfoVerList.CurrentItem != null && !"".Equals(InfoVerList.CurrentItem) && LineList.CurrentItem != null && !"".Equals(LineList.CurrentItem) && StationList.CurrentItem != null && !"".Equals(StationList.CurrentItem))
            {
                flag = true;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("信息填写不全");
                flag = false;
            }
            return flag;
        }

        private void DelParam()
        {
            try
            {
                if (ParaCollectionDetail.CurrentItem != null)
                {
                    Para3084 param = ParaCollectionDetail.CurrentItem as Para3084;
                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "3084参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = paramDao.DeleteObject(param, param.para3084Pk.infoVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

                            ReflashDataGrid();

                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch (Exception e)
            {
            }
        }

        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        private void ReflashDataGrid()
        {
            int infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(infoVer));
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = paramDao.GetAllVersion();
            int newVersion = paramDao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        void LineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (LineList.CurrentItem != null)
            {
                CurrentLine currentLineSelected = LineList.CurrentItem as CurrentLine;
                StationList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
            }
        }

        private void CopyCurrentVersion()
        {
            //查询当前版本
            ObservableCollection<Para3084> currentVersionParam = new ObservableCollection<Para3084>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = paramDao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para3084 param = null;
                Para3084Pk paramPk = null;
                int newVersion = paramDao.GetMaxID() + 1;
                foreach (Para3084 item in currentVersionParam)
                {
                    param = new Para3084();
                    paramPk = new Para3084Pk();
                    //编辑参数
                    paramPk.infoVer = Int16.Parse(newVersion.ToString());
                    paramPk.lineid = item.para3084Pk.lineid;
                    paramPk.statnid = item.para3084Pk.statnid;
                    param.para3084Pk = paramPk;
                    param.filename = item.filename;
                    param.length = item.length;
                    param.XPoint = item.XPoint;
                    param.YPoint = item.YPoint;
                    param.width = item.width;
                    param.resv2 = item.resv2;
                    param.recNum = item.recNum;
                    param.resv11 = item.resv11;
                    param.resv12 = item.resv12;
                    param.resv13 = item.resv13;
                    param.resv14 = item.resv14;
                    param.stationcount = item.stationcount;


                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }
    }
}
